#!/bin/bash
#export PYTHONWARNINGS="ignore::UserWarning"
#source /data/ais/conda/yes/bin/activate
#conda activate rag-core-policy
git pull
# 检查并杀掉之前启动的runserver.py进程
if pgrep -f "python -u runserver.py" >/dev/null; then
    pkill -f "python -u runserver.py"
    sleep 3
    echo "旧的 runserver.py 进程已终止"
fi
sleep 1
# 启动新的 runserver.py 进程
echo "启动新的 runserver.py 进程..."
nohup python -u runserver.py >> log.out 2>&1 &
sleep 5
# 确认进程是否启动成功，重试机制
RETRY_COUNT=0
MAX_RETRIES=3
SLEEP_INTERVAL=5

while ! pgrep -f "python -u runserver.py" >/dev/null; do
    if [ $RETRY_COUNT -ge $MAX_RETRIES ]; then
        echo "runserver.py 启动失败，请检查日志 log.out"
        exit 1
    fi
    echo "runserver.py 启动失败，正在重试... ($((RETRY_COUNT + 1))/$MAX_RETRIES)"
    RETRY_COUNT=$((RETRY_COUNT + 1))
    nohup python -u runserver.py > log.out 2>&1 &
    sleep $SLEEP_INTERVAL
done

echo "runserver.py 启动成功"